const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
import regeneratorRuntime from './regenerator-runtime/runtime'
const wxRequest = async (url, params = {}) => {
  // Object.assign(params, {
  //   token: wx.getStorageSync('token')
  // })
  // 所有的请求，header默认携带token
  let header ={
    'authorization': params.authorization
  }
  let data = params.data || {}
  let method = params.method || 'GET'
  // hideLoading可以控制是否显示加载状态
  if (!params.hideLoading) {
    wx.showLoading({
      title: '加载中...',
    })
  }
  let res = await new Promise((resolve, reject) => {
    wx.request({
      url: url,
      method: method,
      data: data,
      header: header,
      success: (res) => {
        if (res && res.statusCode == 200) {
          resolve(res.data)
        } else {
          reject(res)
        }
      },
      fail: (err) => {
        reject(err)
      },
      complete: (e) => {
        wx.hideLoading()
      }
    })
  })
  return res
}

// export {
//   wxRequest
// }

const wxUpload = async (url, params = {}) => {
  Object.assign(params, {
    token: wx.getStorageSync('token')
  })
  // 所有的请求，header默认携带token
  // let header = params.header || {
  //   'authorization': authorization
  // }
  let header={
    'authorization': params.authorization
  }
  // let data = params.data || {}
  let file =params.file
  // let method = params.method || 'GET'
  // hideLoading可以控制是否显示加载状态
  // if (!params.hideLoading) {
  //   wx.showLoading({
  //     title: '加载中...',
  //   })
  // }
  let res = await new Promise((resolve, reject) => {
    wx.uploadFile({
      url: url,
      filePath: file,
      name: 'files[]',
      header: header,
      success: (res) => {
        if (res && res.statusCode == 200) {
          resolve(res.data)
        } else {
          reject(res)
        }
      },
      fail: (err) => {
        reject(err)
      },
      complete: (e) => {
        wx.hideLoading()
      }
    })
  })
  return res
}









module.exports = {
  formatTime: formatTime,
  wxUpload: wxUpload,
  wxRequest: wxRequest
}
